import 'package:xws/helpers/as_t.dart';
import 'package:xws/models/loc_obj.dart';
import 'package:xws/utils/data_util.dart';
import 'package:xws/utils/storage_util.dart';

/// 发布物品对象
class PubObj {
  PubObj({
    required this.k,
    this.age,
    this.city,
    this.count,
    this.description,
    this.latitude,
    this.longitude,
    this.name,
    this.photoUrlList = const <String>[],
    this.recordUrl,
    this.region,
    this.videoUrl,
    this.recordInd,
    this.originalPrice,
  });

  String k;
  String? age;
  String? city;
  int? count;
  String? description;
  double? latitude;
  double? longitude;
  String? name;
  late List<String> photoUrlList;
  String? recordUrl;
  String? region;
  String? videoUrl;
  double? recordInd;
  double? originalPrice;

  String? get reg {
    if (city == null && region == null) return null;

    return (city ?? '') + ' ' + (region ?? '');
  }

  Map<String, dynamic> toJson({bool withRecord = false}) {
    name = description ?? '';

    name = removeEmoji(name!);
    if (name!.length > 8) name = name!.substring(0, 8);

    return <String, dynamic>{
      if (age != null) 'age': age,
      if (city != null && city!.isNotEmpty) 'city': city,
      if (count != null) 'count': count,
      if (description != null) 'description': description,
      if (latitude != null && latitude != 0) 'latitude': latitude,
      if (longitude != null && longitude != 0) 'longitude': longitude,
      'name': name,
      if (photoUrlList.isNotEmpty) 'photoUrlList': photoUrlList,
      if (recordUrl != null) 'recordUrl': recordUrl,
      if (region != null) 'region': region,
      if (videoUrl != null) 'videoUrl': videoUrl,
      if (withRecord && recordInd != null) 'recordInd': recordInd,
      if (originalPrice != null) 'originalPrice': originalPrice,
    };
  }

  void setLocation(LocObj? loc) {
    city = loc?.city;
    latitude = loc?.latitude;
    longitude = loc?.longitude;
    region = loc?.district;
  }

  String check() {
    if (description == null || description!.isEmpty) return '请填写物品描述';
    if (photoUrlList.isEmpty) return '至少上传一张图片';
    if (age == null) return '请选择物品成色';

    // if (city == null) return '请选发布地点';
    // if (latitude == null) return '请选发布地点';
    // if (longitude == null) return '请选发布地点';

    if (count == null) return '请选物品数量';

    return 'success';
  }

  /// 从map读取数据
  void fromMap(Map<String, dynamic>? data) {
    if (data == null || data.isEmpty) return;

    age = asNullT<String?>(data['age']);
    city = asNullT<String?>(data['city']);
    count = asNullT<int?>(data['count']);
    description = asNullT<String?>(data['description']);
    latitude = asNullT<double?>(data['latitude']);
    longitude = asNullT<double?>(data['longitude']);
    name = asNullT<String?>(data['name']);
    photoUrlList = data['photoUrlList'] as List<String>? ?? <String>[];
    recordUrl = asNullT<String?>(data['recordUrl']);
    region = asNullT<String?>(data['region']);
    videoUrl = asNullT<String?>(data['videoUrl']);
    recordInd = asNullT<double?>(data['recordInd']);
    originalPrice = asNullT<double?>(data['originalPrice']);
  }

  /// 保存到本地
  void save() => setV(k, toJson(withRecord: true));

  /// 从本地读取
  void init() => fromMap(Map<String, dynamic>.from(getV(k) ?? <dynamic, dynamic>{}));

  /// 清空
  void clear() => setV(k, null);
}
